package libnetdb.dm.assoc.base;
import java.util.*;


public class IndexManager {
    private static IndexManager singleton = null;    
    private LinkedHashMap nameMap = new LinkedHashMap();
    
    private IndexManager(){        
    }
    
    public static synchronized IndexManager getInstance(){
        if(singleton == null){
            singleton = new IndexManager();
            singleton.loadIndexes();            
        }
        return singleton;
    }
    
    private void loadIndexes() {
        try{
            Properties prop = new Properties();
            prop.load(IndexManager.class.
                    getResourceAsStream("indexes.prop"));
            for(int i=0; true; ++i){
                String name = prop.getProperty("IndexProvider" + i);
                if(name == null) break;
                Class.forName(name);    
            }
        }catch(Exception e){
        }
    }
    
    public void register(IndexBuilder builder){
        nameMap.put(builder.getName(), builder);
    }
    
    public Iterator builderIterator(){
        return nameMap.values().iterator();
    }
    
    public RuleIndex createIndex(String indexName){
        IndexBuilder builder = (IndexBuilder) nameMap.get(indexName);
        return builder == null ? null : builder.createIndex();
    }
    
}
